package cn.doctorpda.conference.update;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import org.json.JSONObject;

/**
 * 更新封装视图的调用入口
 * Created by jiangxiufeng on 16/8/5.
 */
public class BaiyyyUpdateAgent {

    public static void checkUpdate(final FragmentActivity fragmentActivity){
        checkUpdate(fragmentActivity,false);
    }

    public static void checkUpdateIgnore(final FragmentActivity fragmentActivity){
        checkUpdate(fragmentActivity,true);
    }

    //ignore：是否强制检查更新
    private static void checkUpdate(final FragmentActivity fragmentActivity,final boolean ignore){
        HttpUtils httpUtils=new HttpUtils();
        RequestParams params = new RequestParams();
        JSONObject object = new JSONObject();
        try {
            object.put("client_os", "Android");
            object.put("cur_channel", BaiyyyUpdateConfig.channel);
            object.put("app_key", BaiyyyUpdateConfig.APP_KEY);
            object.put("versionCode", BaiyyyUpdateUtils.getVerisionCode(fragmentActivity)+"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String data = BaiyyyAESUtil.encrypt(object.toString(), BaiyyyUpdateConfig.AES_KEY);
        params.addBodyParameter("source", BaiyyyUpdateConfig.sourse);
        params.addBodyParameter("data", data);

        httpUtils.send(HttpRequest.HttpMethod.POST, BaiyyyUpdateConfig.UPDATA, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> objectResponseInfo) {
                BaiyyyUpdateInfo updateInfo;
                try {
                    JSONObject jsonObject = new JSONObject(objectResponseInfo.result);
                    if (jsonObject.getInt("code") == 0) {
                        String data = BaiyyyAESUtil.decrypt(jsonObject.getString("data"), BaiyyyUpdateConfig.AES_KEY);
                        updateInfo = BaiyyyGsonUtils.fromJson(data, BaiyyyUpdateInfo.class);
                        if (BaiyyyUpdateUtils.getVerisionCode(fragmentActivity) < updateInfo.getVersion_code()) {
                            BaiyyyUpdateDialogFragment dialogFragment = new BaiyyyUpdateDialogFragment();
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("updateInfo", updateInfo);
                            dialogFragment.setArguments(bundle);
                            dialogFragment.show(fragmentActivity.getSupportFragmentManager(), null);
                        }
                    } else if (jsonObject.getInt("code") == 1) {
                        //已是最新版本
                        if (ignore){
                            Toast.makeText(fragmentActivity,"已经是最新版本",Toast.LENGTH_SHORT).show();
                        }
                    }

                } catch (Exception e) {
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.i("s",s);
            }
        });
    }


}
